package com.itheima.controller.setmeal;


import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.setmeal.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author 胡桂明
 * @since 2021/11/4 11:48
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    /**
     * 注入service层
     */
    @Reference
    private SetmealService setMealService;

    /**
     * 新增
     * @param setmeal   前端json获取数据
     */
    @PostMapping("add")
    public Result save(@RequestBody Setmeal setmeal) {
        try {
            setMealService.save(setmeal);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 删除
     * @param id    前端获取要删除的id
     * @return      返回结果
     */
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable int id) {
        try {
            setMealService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false, message);
        }
    }

    /**
     * 修改
     * @param setmeal   前端json获取数据
     */
    @PostMapping("update")
    public Result update(@RequestBody Setmeal setmeal) {
        try{
            setMealService.update(setmeal);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 按ID查找-修改用
     * @param id    查询条件
     * @return      返回结果
     */
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable String id) {
        try{
            Setmeal byId = setMealService.findById(id);
            return new Result(true, "MessageConstant.QUERY_CHECKITEM_SUCCESS", byId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 查询全部
     * @param queryPageBean 分页数据
     * @return  返回查询结果
     */
    @PostMapping("findPage")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean) {
        return setMealService.findAll(queryPageBean);
    }

    /**
     * 文件上传
     * @param imgFile   图片信息
     * @return          返回结果
     */
    @RequestMapping("upload.do")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);
        String fileName = UUID.randomUUID().toString() + suffix;
        //将图片保存到七牛云
        try{
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
}
